<include file="header"/>
<body>
<div class="wraper">
	<div class="head clearfix">
		<a href="javascript:history.go(-1);" class="iconfont icon-fanhui72 pull-left"></a>
		<span class="head-name pull-left">预约篮</span>
		<if condition="$data">
			<a href="javascript:;" class="pull-left text-right" id="edit">修改</a>
		</if>
	</div>
	<div class="ordersbox">
	<if condition="$data">
		<volist name="data" id="vo">

			<div class="tb-mode orderslist" id="cart_list{BEESCRM:$vo.cart_id}">
				<div class="white-block">
					<div class="tb-cell info-left">
						<div class="image-box">
							<a href="{BEESCRM::U('base/bonsai/detail', array('wechat_id' => $wechat_id, 'goods_id'=>$vo['goods_id'],'origin' => $origin))}">
								<img src="{BEESCRM:$vo.goods.goods_image}"/>
							</a>
						</div>
					</div>
					<div class="tb-cell vertical-top">
						<p class="text-green goods-name">{BEESCRM:$vo.goods.goods_name}</p>

						<if condition="$vo['type'] eq 2">
							<p class="sub-contain">会场布置: {BEESCRM:$vo.goods.meetingplace_price} 元/盆/场</p>
							<elseif condition="$vo['type'] eq 3"/>
							<p class="sub-contain">长期摆放: {BEESCRM:$vo.goods.long_price} 元/盆/天</p>
						</if>

						<p class="sub-contain">预约数量：<span id="goods_num">{BEESCRM:$vo.goods_num}</span> 盆</p>
						<p class="sub-contain">小计：<span class="text-red">￥<span class="cart_price{BEESCRM:$vo.cart_id}" id="cart_price">{BEESCRM:$vo.cart_price}</span> <if condition="$vo['type'] eq 2">元/场<else/>元/天</if></span></p>

						<div class="quantity tb-mode hide">
							<span class="iconfont icon-iconfont55 tb-cell change-num-btn" data-status="sub" data-id="{BEESCRM:$vo.cart_id}" style="cursor:pointer;"></span>
							<input type="number" class="buy-count" value="{BEESCRM:$vo.goods_num}" data-status="input" autocomplete="on" data-id="{BEESCRM:$vo.cart_id}">
							<span class="iconfont icon-jia tb-cell change-num-btn" data-status="plus" data-id="{BEESCRM:$vo.cart_id}" style="cursor:pointer;"></span>
						</div>
					</div>
					<input name="cartId" class="cartId" type="hidden" value="{BEESCRM:$vo.cart_id}"/>
				</div>
				<a href="javascript:;" class="tb-cell delete-list delete" data-cartid="{BEESCRM:$vo.cart_id}">删除</a>
			</div>
		</volist>
		<input type="hidden" value="" id="delCartId"/>
		<else/>
		</if>
	</div>

	<if condition="$data">
		<div class="fixed-accounts">
			<div class="tb-mode">
				<div class="tb-cell" id="wholeFee">
					预计总费用 <span class="text-red"><span id="goodsprice">{BEESCRM:$goodsPrice}</span> 元</span>
				</div>
				<div class="tb-cell">
					<div class="pull-right"><a class="btn" href="{BEESCRM::U('base/bonsai/cart', array('wechat_id'=> $wechat_id,'theme'=>1,'origin'=>$origin))}" data-href="{BEESCRM::U('base/bonsai/cart', array('wechat_id'=> $wechat_id,'theme'=>1,'origin'=>$origin))}">立即预约</a></div>
				</div>
			</div>
		</div>
	</if>
	<include file="nav" />
</div>
<include file="commonJs"/>
<script>
	var numReg = /^\+?[1-9][0-9]*$/;
	var nav_h = $('.nav').height();
	$('.wraper').css('paddingBottom',nav_h+8);
	/*var img_h = $('.image-box img').height();
	$('.image-box img').css('marginTop',-img_h/2);*/
	var wechat_id = "{BEESCRM:$wechat_id}";
	var origin = "{BEESCRM:$origin}";
	$(document).ready(function () {
		$(document).on('click','#edit',function(){
			$('.sub-contain').hide();
			$('.quantity').addClass('tb-show');
			$('.delete-list').addClass('cell-show');
			$('.btn').addClass('gray-btn').attr('href', 'javascript:;');
			$('#wholeFee').hide();
			$(this).html('完成');
			$(this).attr('id','complete');
		});

		$(document).on('click','#complete',function(){
			$('.sub-contain').show();
			$('.quantity').removeClass('tb-show');
			$('.delete-list').removeClass('cell-show');
			$('.btn').removeClass('gray-btn');
			var url = $('.btn').data('href');
			$('.btn').attr('href', url);
			$('#wholeFee').show();
			$(this).html('修改');
			$(this).attr('id','edit')
			
			var data = {},cartId = [],goodsNum = [];
			$('input[name="cartId"]').each(function(){
				cartId.push($(this).val());
			});
			$('input[type="number"]').each(function(){
				var goodsNums = $(this).val();
				goodsNum.push($(this).val());
			});
			
			//console.log(cartId);console.log(goodsNum);
			 if((cartId.length==0) || (goodsNum.length==0)){
        		//$.alert('error', '预约篮商品为空');
        		$('.fixed-accounts').remove();
    			$('#wholeFee').hide();
    			location.reload();
        		return false;
       		 }
			
			data.cardId = cartId;
			data.goodsNum = goodsNum;

			var json = new Object();
			json.data = data;
			json.method = 'post';
			json.url = "{BEESCRM::U('base/bonsai/updateCart', array('wechat_id' => $wechat_id, 'origin' => $origin))}";
			//console.log(json);
			$.getAjax(json, true, change, '');


			function change(result){
				//var result = $.parseJSON(result);
				var data = result.data;
				var str = '';
				var total = 0;
				if(data.length > 0) {
					$.each(data,function(i,k){
						str += '<div class="tb-mode orderslist" id="cart_list'+ k.cart_id+'"> <div class="white-block"> <div class="tb-cell info-left">'+
						'<div class="image-box">' 
						+'<a href="/base/bonsai/detail/wechat_id/'+wechat_id+'/goods_id/'+k.goods_id+'/origin/'+origin+'">'
						+'<img src="'+ k.goods.goods_image+'">'
						+'</a>'
						+'</div> </div> <div class="tb-cell vertical-top"><p class="text-green goods-name">'+ k.goods.goods_name+'</p>'+
						'<p class="sub-contain" style="display: block;">会场布置: '+ k.goods.meetingplace_price+' 元/盆/场</p><p class="sub-contain" style="display: block;">预约数量：<span id="goods_num">'+k.goods_num+'</span> 盆</p>'+
						'<p class="sub-contain" style="display: block;">小计：<span class="text-red">￥<span class="cart_price3" id="cart_price">'+ k.cart_price+'</span> 元</span></p>'+
						'<div class="quantity tb-mode hide"> <span class="iconfont icon-iconfont55 tb-cell change-num-btn" data-status="sub" data-id="'+ k.cart_id+'"></span>'+
						'<input type="number" value="'+ k.goods_num+'" data-status="input" autocomplete="on" data-id="'+ k.cart_id+'"> <span class="iconfont icon-jia tb-cell change-num-btn" data-status="plus" data-id="'+ k.cart_id+'"></span>'+
						'</div> </div>'
						+'<input name="cartId" class="cartId" type="hidden" value='+k.cart_id+'/>'
						+'</div> <a href="javascript:;" class="tb-cell delete-list delete" data-cartid="'+k.cart_id+'">删除</a> </div>';
						total += parseInt(k.cart_price);
					});
					$(".ordersbox").html(str);
					$("#goodsprice").html(total);
					//$('.image-box img').css('marginTop',-img_h/2);
				}
			}
		})
	});

$(document).ready(function(){
	$(document).on('click','.change-num-btn',function(){
		var id = $(this).data('id'),
				status = $(this).data('status'),
				elem = $(this).parent('.quantity').find('input');
		renderNumVal(id,status,elem);
	});

	$(document).on('change', '.buy-count', function () {
		var id = $(this).data('id'),
				status = $(this).data('status'),
				elem = $(this).parent('.quantity').find('input');
		renderNumVal(id,status,elem);
	});

	function renderNumVal(id, status,elem) {
		var initNum;
		if(elem.data('id')==id){
			initNum = elem.val();
		}

		if (status == 'plus') {
			initNum++;
		} else if (status == 'sub') {
			if (initNum <= 1) {
				initNum = 1;
				return false;
			}
			initNum--;
		} else if (status == 'input') {
			initNum = elem.val();
			if (initNum <= 1) {
				initNum = 1;
				elem.val(initNum);
			}
		}
		elem.val(initNum);
		
		/* $.get("{BEESCRM::U('base/bonsai/ceshi', array('wechat_id'=>$wechat_id, 'origin' => $origin))}", {
			cart_id: id,
			goods_num: initNum
		}); */
	}
	});
	//删除商品
	$(document).on('click', '.delete', function(){
		var cartId = $(this).data('cartid');
		if(!cartId){
			$.alert('error', '此条商品请求参数有误');
		}
		$('#delCartId').val(cartId);
		  $.confirm('确认删除此商品？', yesDo);
	});
	function yesDo(){
		var delCartId = $('#delCartId').val(), jsons = new Object(), data = {};
		data.delCartId = delCartId;
		jsons.data = data;
		jsons.method = 'post';
		jsons.url = "{BEESCRM::U('base/bonsai/delCart', array('wechat_id' => $wechat_id, 'origin' => $origin))}";
		//console.log(jsons);
		$.getAjax(jsons, true, delSRemove, delFRemove);
		
	}
	
	function delSRemove(data){
		$('#cart_list'+data.data).remove();
		$.alert('success', data.message);
	}
	
	function delFRemove(data){
		$.alert('error', data.message);
	}
</script>
</body>
</html>
